package cn.zjnu.controller;

import cn.zjnu.domain.User;
import cn.zjnu.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController   // 该标签表示： 1. 该类是@Controller ； 2. 该类采用REST风格开发 。
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private LoginService loginService;
    @PostMapping
    public Result save(@RequestBody User user){
        System.out.println(user.getUsername()+" is creating account ... ");
        boolean flag = loginService.signUp(user);
        return new Result(user,flag ? Code.SignUp_OK:Code.SignUp_ERR,flag ? "创建用户成功":"存在相同用户名，注册失败");
    }
    @GetMapping
    public Result login(@RequestBody User user){
        System.out.println(user.getUsername() + " is trying to login ...");
        boolean flag = loginService.loginIn(user);
        return new Result(user,flag ? Code.LoginIn_OK:Code.LoginIn_ERR,flag ? "登陆成功":"登陆失败");
    }
    @PutMapping
    public Result changePassword(@RequestBody List<User> users){
        System.out.println(users.get(0).getUsername() + "is trying to change password");
        boolean flag = loginService.changePassword(users.get(0), users.get(1) );
        return new Result(users.get(0),flag ? Code.Change_OK:Code.Change_ERR,flag ? "修改密码成功":"修改密码失败");
    }
}
